package butines.core.gui.style;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import butines.core.GameImage;

public class Fonts {

	private static BufferedImage img = null;
	
	public static FontMetrics getFontMetrics(Font font) {
		if (img == null) {
			img = GameImage.createCompatibleImage(1, 1, Transparency.BITMASK);
		}
		Graphics2D g2d = null;
		try {
    		g2d = (Graphics2D) img.getGraphics();
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    		return (font == null) ? g2d.getFontMetrics() : g2d.getFontMetrics(font);
		} finally {
		    if (g2d != null) {
		        g2d.dispose();
		    }
		}
	}
	
}
